package com.study.controller;

import com.study.bean.entity.DormitoryAdmin;
import com.study.service.AdminService;
import com.study.service.impl.AdminServiceImpl;
import com.study.util.Result;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
import java.util.UUID;

@WebServlet("/upload")
public class UploadController extends BaseController{
    private AdminService  adminService = new AdminServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UploadController.service");
        //1.DiskFileItemFactory硬盘文件项工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //设置缓存  1M=1024KB  1KB=1024B
        factory.setSizeThreshold(10 * 1024 * 1024);
        String tempPath = getServletContext().getRealPath("temp");
        //设置临时文件
        factory.setRepository(new File(tempPath));

        //2.ServletFileUpload文件上传的工具类
        ServletFileUpload  servletFileUpload = new ServletFileUpload(factory);
        //设置文件上传的编码
        servletFileUpload.setHeaderEncoding("UTF-8");
        String fileName = "";
        if (ServletFileUpload.isMultipartContent(req)) {
            try {
                List<FileItem> fileItems = servletFileUpload.parseRequest(req);
                for (FileItem fileItem : fileItems) {
                    //如果是普通表单项 name: zhangsan
                    if (!fileItem.isFormField()) {//文件上传
                        //a.png
                        String name = fileItem.getName();
                        //041838957bbe4759847db881c7f21adc
                        String uuid = UUID.randomUUID().toString().replace("-", "");
                        //png
                        String extension = FilenameUtils.getExtension(name);
                        //041838957bbe4759847db881c7f21adc.png
                        fileName = uuid + "." + extension;

                        InputStream inputStream = fileItem.getInputStream();
                        OutputStream outputStream = new FileOutputStream("D:\\mypic\\Avatar\\" + fileName);
                        IOUtils.copy(inputStream, outputStream);
                        //删除临时文件
                        fileItem.delete();
                        HttpSession session = req.getSession();
                        DormitoryAdmin admin = (DormitoryAdmin) session.getAttribute("admin");
                        System.out.println(admin);
                        DormitoryAdmin newadmin =adminService.updateAvatar(admin.getAdminId(),fileName);
                        //更新session
                        session.setAttribute("admin",newadmin);
                    }
                }
            } catch (FileUploadException e) {
                throw new RuntimeException(e);
            }
        }

        toJSON(resp, Result.ok("上传成功", fileName));
    }


}
